package com.base.common.convert;

import com.google.common.base.Preconditions;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;


/**
 * 装饰者模式（或者理解为静态代理）
 */
public class RabbitMessageConverter implements MessageConverter {

    private GenericMessageConverter delegate;

//    private final String defalutExprie = String.valueOf(24*60*60*1000);

    public RabbitMessageConverter(GenericMessageConverter genericMessageConverter){
        Preconditions.checkNotNull(genericMessageConverter);
        this.delegate = genericMessageConverter;
    }

    @Override
    public Message toMessage(Object o, MessageProperties messageProperties) throws MessageConversionException {
//        messageProperties.setExpiration(defalutExprie);
        com.base.api.Message message=(com.base.api.Message)o;
        messageProperties.setDelay(message.getDelayMills());
        return this.delegate.toMessage(o,messageProperties);
    }

    @Override
    public Object fromMessage(Message message) throws MessageConversionException {
        com.base.api.Message msg = (com.base.api.Message) this.delegate.fromMessage(message);
        return msg;
    }
}
